package com.example.shorg.controller;

import com.example.shorg.dataobject.ProductInfo;
import com.example.shorg.service.ProductService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import java.util.Map;

@Controller
@Slf4j
@RequestMapping("/product")
public class ShorgProductController {

    @Autowired
    private ProductService productService;

    @GetMapping("")
    public ModelAndView productList(@RequestParam(value = "page") Integer page,
                                    @RequestParam(value = "size") Integer size,
                                    Map<String, Object> map){
        PageRequest pageRequest = new PageRequest(page, size);
        Page<ProductInfo> pageInfo = productService.findAll(pageRequest);
        map.put("productInfo", pageInfo);
        map.put("currentPage", page);
        map.put("size", size);
        return new ModelAndView("/product/list",map);
    }
}
